package com.power.sifa.approve.service.impl;

import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.approve.dao.Apl_admre_auditDao;
import com.power.sifa.approve.model.Apl_admre_audit;
import com.power.sifa.approve.model.pageInfo.Apl_admre_auditPageInfo;
import com.power.sifa.approve.service.Apl_admre_auditService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 *  行政复议相关审批表业务层实现
 */
@Service
public class Apl_admre_auditServiceImpl extends BaseServiceImpl implements Apl_admre_auditService {

    @Autowired
    private Apl_admre_auditDao dao;

    @Override
    public Apl_admre_audit getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Apl_admre_audit apl_admre_audit) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (apl_admre_audit.getPrimaryKey() != null) {
        	 Apl_admre_audit apl_admre_auditfDb = dao.findById(apl_admre_audit.getPrimaryKey()).orElse(null);
             if(apl_admre_auditfDb != null) {
                BeanUtils.copyProperties(apl_admre_audit, apl_admre_auditfDb);
                dao.save(apl_admre_auditfDb);
                return;
             }
        }
        dao.save(apl_admre_audit);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Apl_admre_audit> findAll(Apl_admre_auditPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }

    @Override
	public List<Apl_admre_audit> findAll() {
		return dao.findAll();
	}

	@Override
	public void saveAll(List<Apl_admre_audit> list) {
		dao.saveAll(list);
	}

    @Override
    public void updateStatus(String id, Integer status) {
        this.dao.updateStatus(id, status);
    }

    /**
     * 根据applyid查询获取行政复议相关审批表对象
     * @param applyid
     * @return
     */
    @Override
    public List<Apl_admre_audit> listAdmreAuditByApplyid(String applyid) {
        Specification<Apl_admre_audit> specification = (root, query, criteriaBuilder) -> query.where(
                criteriaBuilder.equal(root.get("applyid"), applyid)
        ).getRestriction();
        return dao.findAll(specification);
    }

    @Override
    public Apl_admre_audit getLastChildAudit(String parentApplyid, Integer procType) {
        return dao.getLastChildAudit(parentApplyid, procType);
    }

    @Override
    public void updateQuedate(String applyid, String quedate) {
        this.dao.updateQuedate(applyid, quedate);
    }

}
